<?php
/**
 * @package scottlib.core
 * @author Aaron Collegeman aaroncollegeman.com
 * @version 1.0.0-BETA
 * 
 * Copyright (C)2009 Collegeman.net, LLC.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
 */

///////////////////////////////////////////////////////////////////////////////
// This file provides a standard result set object and implementations.
///////////////////////////////////////////////////////////////////////////////

/**
 * Standard result set interface.
 */
abstract class slResultSet implements Iterator, ArrayAccess {
	
	protected $resource;
	
	protected $position = -1;
	
	protected $active = null;
	
	protected $mode = 'object';
	
	/**
	 * @param $resource A reference to a query resource 
	 */
	function __construct($resource) {
		$this->resource = $resource;
	}
	
	/**
	 * @see ArrayAccess::offsetExists()
	 */
	function offsetExists($offset) {
		assert_is_numeric($offset, "Array index must be numeric");
		return ($offset-1 > $this->count());	
	}
	
	/**
	 * @see ArrayAccess::offsetGet()
	 */
	function offsetGet($offset) {
		assert_is_numeric($offset, "Array index must be numeric");
		while ($this->position < $offset)
			$this->next();
		return $this->current();
	}
	
	/**
	 * @see ArrayAccess::offsetSet()
	 */
	function offsetSet($offset, $value) {
		throw new Exception("slResultSet objects are immutable (read-only).");
	}
	
	/**
	 * @see ArrayAccess::offsetUnset()
	 */
	function offsetUnset($offset) {
		throw new Exception("slResultSet objects are immutable (read-only).");
	}	
	
	/**
	 * @see Iterator::current()
	 */
	function current() {
		return $this->active;
	}
	
	/**
	 * @see Iterator::valid()
	 */
	function valid() {
		return ($this->active !== null && $this->active !== false);	
	}
	
	/**
	 * @see Iterator::key()
	 */
	function key() {
		return $this->position;	
	}
	
	/**
	 * Switches the result set wrapper into object mode, in which records are fetched from the result set as objects
	 * @see mysql_fetch_object()
	 * @since 0.1.0
	 */
	function objects() {
		$this->mode = 'object';	
		return $this;
	}
	
	/**
	 * Switches the result set wrapper into associative array mode, in which records are fetched from the result set as hashes
	 * @since 0.1.0
	 */
	function hashes() {
		$this->mode = 'assoc';	
		return $this;
	}
	
	/**
	 * Switches the result set wrapper into array mode, in which records are fetched from the result set as arrays
	 * @see mysql_fetch_array()
	 * @since 0.1.0
	 */
	function arrays() {
		$this->mode = 'array';
		return $this;
	}
	
	/**
	 * @return The number of records held in the underyling result set.
	 */
	abstract function count();
	
	/**
	 * @return The number of fields (i.e., columns) represented in the underlying result set.
	 */
	abstract function fieldCount();
	
	/**
	 * @return The first record stored in the underlying result set, or when the result set is empty, null.
	 */
	function first() {
		if ($this->count() >= 1) {
			if ($this->position !== 0)
				$this->rewind();
			return $this->current();
		}
		else
			return null;
	}
	
	/**
	 * @param $default
	 * @return The value of the first column of the first record in the result set
	 */
	function val($default = null) {
		if ($record = $this->arrays()->first()) {
			return $record[0];
		}
		else
			return $default;
	}
	
}

final class slMySqlResultSet extends slResultSet {
	
 	function rewind() {
		@mysql_data_seek($this->resource, 0);
		$this->position = -1;
		$this->next();
	}	
	
	function next() {
		if ($this->mode == 'object') 
			$this->active = @mysql_fetch_object($this->resource);
			
		else if ($this->mode == 'assoc')
			$this->active = @mysql_fetch_assoc($this->resource);
			
		else if ($this->mode == 'array')
			$this->active = @mysql_fetch_array($this->resource);

		$this->position++;
	}
	
	function count() {
		return @mysql_num_rows($this->resource);
	}
	
	function fieldCount() {
		return @mysql_num_fields($this->resource);	
	}
	
}